package com.garrett.demo.sys.dao;

import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;

import java.util.Arrays;

public class UserDataBaseDemo implements UserDao {
    private int MaxAccount_num = 20,MaxStudent_num = 20 ;
    private int Account_num = 0, Student_num = 0;
    private SimsStudent[] students = new SimsStudent[MaxStudent_num];
    private Account[] accounts = new Account[MaxAccount_num];

    public SimsStudent[] getStudents() {
        return students;
    }

    public Account[] getAccounts() {
        return accounts;
    }

    /**
     * 根据学号获取学生信息
     * @param stuId 学号
     * @return 成功-用户信息 失败-null
     */

    @Override
    public SimsStudent getStudentById(String stuId) {
        for (int i = 0 ; i < Student_num ; i ++){
            if(students[i].getStudentId().equals(stuId)){
                return students[i];
            }
        }
        return null;
    }

    /**
     * 根据登录账号获取账户资源信息
     * @param account 账户名
     * @return 成功-账户信息 失败-null
     */

    @Override
    public Account getUserByAccount(String account) {
        for(int i = 0; i < Account_num; i ++){
            if(accounts[i].getAccount().equals(account)){
                return accounts[i];
            }
        }
        return null;
    }

    /**
     * 注册账户信息
     * @param account 账户
     * @return 1-成功插入一条数据 0-数据创建失败
     */

    @Override
    public int addAccount(Account account) {
        if(account != null && Account_num < MaxAccount_num) {
            accounts[Account_num++] = account;
            return  1;
        }
        return 0;
    }

    /**
     * 添加用户信息
     * @param userInfo 用户信息
     * @return 1-成功插入一条数据 0-数据创建失败
     */

    @Override
    public int createUser(SimsStudent userInfo) {
        if(userInfo != null && Student_num < MaxStudent_num ) {
            students[Student_num++] = userInfo;
            return  1;
        }
        return 0;
    }

    /**
     * 获取所有学生信息
     * @return 学生信息列表
     */

    @Override
    public SimsStudent[] getAllStudent() {
        if(Student_num == 0){
            return null;
        }
        return Arrays.copyOfRange(students,0, Student_num);
    }

    public int getStudent_num()
    {
        return Student_num;
    }

    public int getAccount_num()
    {
        return Account_num;
    }

    public void setAccount_num(boolean flag) {if(flag)Account_num--;else Account_num ++;}

    public void setStudent_num(boolean flag) {if(flag)Student_num--;else Student_num ++;}

    /**
     * 根据ID删除账户
     * @param id 账户名
     * @return 成功-1 失败-null
     */

    @Override
    public  int delAccountById(String  id)
    {
        for(int i = 0 ; i < Account_num ; i++) {
            if(accounts[i].getAccount().equals(id)) {
                for (int j = i + 1; j < Account_num; j++)
                    accounts[j - 1] = accounts[j];
                if(i == 1) accounts[i] = null;
                Account_num--;
                return 1;
            }
        }
        return 0;
    }



    /**
     * 根据登录账号修改账户资源信息
     * @param account 账户名
     * @return 成功-1 失败-0
     */

    @Override
    public int modifyAccount(Account account)
    {
        for (int i = 0 ; i < Account_num ; i ++){
            if(accounts[i].getAccount().equals(account.getAccount())){
                accounts[i] = account;
                return 1;
            }
        }
        return 0;
    }


    /**
     * 删除用户信息
     * @param stuId 用户
     * @return 1-成功插入一条数据 0-数据创建失败
     */

    @Override
    public int delStudentInfo(String stuId)
    {
        for (int i = 0 ; i < Student_num ; i ++){
            if(students[i].getStudentId().equals(stuId)){
                for (int j = i + 1; j < Student_num; j++)
                    students[j - 1] = students[j];
                    if(i == 1) students[i] = null;
                    Student_num--;
                    return 1;
            }
        }
        return 0;
    }
    /**
     * 修改学生信息
     *
     * <p>studentId(学号)不可改变，根据主键studentId判断修改</p>
     * <p>例子：{studentId:212006677,studentName:garrett,email:null}</p>
     * <p>根据studentId判断数组中是否存在对应学号，如存在则可以修改除学号以外的对应属性值，否则无效修改</p>
     *
     * @param student 学生信息
     * @return 1-成功 0-失败
     */
    public int modifyStudentInfo(SimsStudent student)
    {
        for (int i = 0 ; i < Student_num ; i ++){
            if(students[i].getStudentId().equals(student.getStudentId())){
                students[i] = student;
                return 1;
            }
        }
        return 0;

    }

}
